//
//  TTTabBarController.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/19.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTTabBarController: UITabBarController {

    let homeTabBar = TTTabBar()
    var coverView : TTCoverView?
    override func viewDidLoad() {
        super.viewDidLoad()
        
// 底部centerButton 点击调用闭包
        
        homeTabBar.centerButotnBlock = {[weak self] in
            
            //            self.presentViewController(UINavigationController(rootViewController: TTComposeController()), animated: true, completion: nil)
            
            self!.coverView = TTCoverView()
            self!.view.addSubview(self!.coverView!)
            
            self!.coverView!.snp_makeConstraints { (make) -> Void in
                
                make.edges.equalTo(self!.view).offset(UIEdgeInsetsZero)
            }
            
            for value in (self?.coverView?.buttonArray)!{
                
                value.addTarget(self, action: "didClickButton:", forControlEvents: UIControlEvents.TouchUpInside)
            }
        }
        
        
// 运行时 设置自定义 tabBar
        
        setValue(homeTabBar, forKey: "tabBar")
        addChildViewController(TTHomeController(), title: "首页", img: "tabbar_home")
        addChildViewController(TTMassageController(), title: "消息", img: "tabbar_message_center")
        addChildViewController(TTDiscoverController(), title: "发现", img: "tabbar_discover")
        addChildViewController(TTAboutMeController(), title: "我", img: "tabbar_profile")
}
// 添加子控制器
    
    func addChildViewController(vc: UIViewController,title:String,img:String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: img)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        vc.tabBarItem.selectedImage = UIImage(named: "\(img)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        addChildViewController(UINavigationController(rootViewController: vc))
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        
    }
    
    /*
    guard let cn = NSClassFromString("\(pn).\(vcName)") as? UIViewController.Type else {
    return
    }
    */
    
    func didClickButton(sender:TTCoverButton){
        
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            
            for value in (self.coverView?.buttonArray)!{
                
                if value == sender{
                    
                    value.transform = CGAffineTransformMakeScale(1.5, 1.5)
                    value.alpha = 0.2
                    
                }else{
                    
                    value.transform = CGAffineTransformMakeScale(0.5, 0.5)
                    value.alpha = 0.2
                }
                
            }
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    for value in (self.coverView?.buttonArray)!{
                    
                    value.transform = CGAffineTransformIdentity
                    value.alpha = 1
                    
                    }
                    
                    }, completion: { (_) -> Void in
                        
                        guard let VcNname = sender.buttonVC else{
                            
                            return
                        }
                        
                        guard let info = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] else{
                            
                            return
                        }
                        
                        guard let cn = NSClassFromString("\(info).\(VcNname)") as? UIViewController.Type else{
                            
                            return
                        }
                        
                        let tepVc = cn.init()
                        
                        self.presentViewController(UINavigationController(rootViewController: tepVc), animated: true, completion: { () -> Void in
                            
                            self.coverView?.removeFromSuperview()
                        })
                })
                
        }
        
    }

}
